package ssn.main.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import ssn.common.code.service.CodeService;
import ssn.common.code.vo.CodeVO;
import ssn.common.highchart.service.HighChartService;
import ssn.daily.service.DailyService;
import ssn.daily.vo.DailyVO;
import ssn.main.service.MainService;
import ssn.main.vo.MainVO;
import ssn.servicearea.service.ServiceAreaService;
import egovframework.rte.fdl.cmmn.AbstractServiceImpl;
import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.fdl.string.EgovStringUtil;

/**  
 * @Class Name : 
 * @Description : 
 * 
 * @author ksh
 * @since 2013. 09.26
 * @version 1.0
 * @see
 */
@SuppressWarnings({"rawtypes","unchecked"})
@Service("mainService")
public class MainServiceImpl extends AbstractServiceImpl implements MainService {
	
	@Resource(name="mainDAO")
	private MainDAO mainDAO; 
	
	@Resource(name = "dailyService")
    private DailyService dailyService;
    
    @Resource(name = "highchartService")
    private HighChartService highchartService;
    
    @Resource(name = "serviceareaService")
    private ServiceAreaService serviceareaService;
    
    @Resource(name = "codeService")
    private CodeService codeService;
    
    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
    
	public Map selectMainInfo(MainVO vo) throws Exception{
		Map resultMap = new HashMap();
		DailyVO dailyVO = new DailyVO();
		dailyVO.setSearchParam(vo);
		List<Map> dailyList = dailyService.selectDailyChart(dailyVO);		// 일지 정보
		//resultMap.put("dailyInfo", mainDAO.selectMainInfo(vo));
		String unusualYn = "N";
		String unusualCont = "";
		if(dailyList != null && dailyList.size() > 0){
			unusualYn = (String)dailyList.get(dailyList.size()-1).get("unusualYn");		// 마지막 특이사항 YN
			unusualCont = (String)dailyList.get(dailyList.size()-1).get("unusualCont");	// 마지막 특이사항 내용
		}
		resultMap.put("mainInfo", mainDAO.selectMainInfo(vo));
		resultMap.put("unusualYn", unusualYn);
		resultMap.put("unusualCont", unusualCont);
		resultMap.put("chartInfo", highchartService.getChart(dailyList, 7));
		return resultMap;
	}
	
	public Map selectAdminMainInfo(MainVO vo) throws Exception{
		Map resultMap = new HashMap();
		List<CodeVO> zoneList = codeService.selectCodeInfoList(propertiesService.getString("scc.codekey.zone"));
		if(EgovStringUtil.isEmpty(vo.getSearchCodeKey())){
			if(zoneList != null && zoneList.size() > 0){
				vo.setSearchCodeKey(zoneList.get(0).getScc_code_key());
			}else{
				vo.setSearchCodeKey("");
			}
		}
		List<Map> dailyList = mainDAO.selectAdminMainChart(vo);		// 일지 정보
		
		/**
		 * 일지 정보를 휴게소별로 나눕니다.
		 * */
		List<List> dailyListTochartList = getDailyListToChartList(dailyList, "areaKey");	
		List<Map> chartInfoList = null;								// 각 차트 Map 을 담을 리스트
		if(dailyListTochartList != null && dailyListTochartList.size() > 0){
			chartInfoList = new ArrayList<Map>();
			for(List<Map> areaChartList : dailyListTochartList){
				Map tempMap = new HashMap();
				tempMap.put("waterQualityChart", highchartService.getWaterQualityChart(areaChartList));
				tempMap.put("unusualYn", (String)areaChartList.get(areaChartList.size()-1).get("unusualYn"));
				chartInfoList.add(tempMap);
			}
		}
		
		resultMap.put("zoneList", zoneList);
		resultMap.put("chartInfoList", chartInfoList);
		resultMap.put("mainInfo", mainDAO.selectAdminMainInfo(vo));
		return resultMap;
	}
	
	public Map selectAdminChartAll(MainVO vo) throws Exception{
		Map resultMap = new HashMap();
		List<CodeVO> zoneList = codeService.selectCodeInfoList(propertiesService.getString("scc.codekey.zone"));
		List<Map> dailyList = mainDAO.selectAdminMainChart(vo);		// 일지 정보
		
		/**
		 * 일지 정보를 휴게소별로 나눕니다.
		 * */
		List<List> dailyListTochartList = getDailyListToChartList(dailyList, "areaKey");	
		List<Map> chartInfoList = null;								// 각 차트 Map 을 담을 리스트
		if(dailyListTochartList != null && dailyListTochartList.size() > 0){
			chartInfoList = new ArrayList<Map>();
			for(List<Map> areaChartList : dailyListTochartList){
				Map tempMap = new HashMap();
				tempMap.put("zoneKey", (String)areaChartList.get(areaChartList.size()-1).get("codeKey"));
				tempMap.put("zoneName", (String)areaChartList.get(areaChartList.size()-1).get("codeName"));
				tempMap.put("waterQualityChart", highchartService.getWaterQualityChart(areaChartList, false));
				tempMap.put("unusualYn", (String)areaChartList.get(areaChartList.size()-1).get("unusualYn"));
				chartInfoList.add(tempMap);
			}
		}
		
		resultMap.put("zoneList", zoneList);
		resultMap.put("chartInfoList", chartInfoList);
		return resultMap;
	}
	
	private List<List> getDailyListToChartList(List<Map> dailyList, String baseKey){
		if(dailyList == null){
			return null;
		}
		List<List> resultList = new ArrayList<List>();
		List<Map> tempList = null;									
		String tempStr = "";
		for(int i=0; i<dailyList.size(); i++){
			Map map = dailyList.get(i);
			if(!tempStr.equals(map.get(baseKey))){
				if(tempList != null && tempList.size() > 0){
					resultList.add(tempList);
				}
				tempList = new ArrayList<Map>();
				tempList.add(map);
				tempStr = (String)map.get(baseKey);
			}else{
				tempList.add(map);
			}
			if(tempList != null && tempList.size() > 0 && dailyList.size()-1 == i){
				resultList.add(tempList);
			}
		}
		return resultList;
	}
}
